package clientUtilGUI;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

import clientMain.Login;
import clientMain.Main;
import clientView.Company_Edit;
import clientView.Company_View;

public class Tab_Company extends JToolBar implements ActionListener {
	private JButton btnCompanyInfo;
	private JButton btnCompanyEdit;

	public Tab_Company(){
		setFloatable(false);
		setOrientation(JToolBar.HORIZONTAL);
		setMargin(new Insets(0, 6, 0, 2));
				
		btnCompanyInfo = new JButton(UtilityGUI.subMenuComInfo, new ImageIcon("src/image/company.png"));
		btnCompanyInfo.setVerticalTextPosition(JButton.BOTTOM);
		btnCompanyInfo.setHorizontalTextPosition(JButton.CENTER);
		btnCompanyInfo.addActionListener(this);
		add(btnCompanyInfo);
		
		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("Company" ) &&
					Login.listAuthorization.get(i).getPermName().equals("Edit")){
				btnCompanyEdit = new JButton(UtilityGUI.subMenuComEdit, new ImageIcon("src/image/paper.png"));
				btnCompanyEdit.setVerticalTextPosition(JButton.BOTTOM);
				btnCompanyEdit.setHorizontalTextPosition(JButton.CENTER);
				btnCompanyEdit.addActionListener(this);
				add(btnCompanyEdit);
				break;
			}
		}
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()== btnCompanyInfo){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabComView);
			if(index == -1){
				Main.jTabbedPane.addTab(new Company_View(), UtilityGUI.tabComView);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
		
		if(e.getSource()== btnCompanyEdit){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabComEdit);
			if(index == -1){
				Main.jTabbedPane.addTab(new Company_Edit(Main.comId), UtilityGUI.tabComEdit);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
	}
}
